package com.ruoyi.framework.mongoDB;

import com.alibaba.dubbo.config.annotation.Service;
import com.witc.common.operateLog.entity.OperateLog;
import com.witc.common.operateLog.entity.OperateLogDTO;
import com.witc.common.operateLog.service.OperateLogService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.regex.Pattern;

@Service(timeout =60000,version = "1.0.0",interfaceClass = OperateLogService.class)
public class OperateLogServiceImpl extends MongoDaoSupport<OperateLog> implements OperateLogService {

    @Override
    public void saveLog(OperateLog operateLog) {
        save(operateLog);
    }

    @Override
    public OperateLog getLog(OperateLog operateLog) {
        OperateLog byId = findById(operateLog.getId());

        return byId;
    }

    @Override
    public List<OperateLog> getLogList(OperateLog operateLog) {
        return findByCondition(operateLog);
    }

    @Override
    public Page<OperateLog> getPage(OperateLogDTO operateLogDTO) {
        Query query = buildQuery(operateLogDTO);
        return findPage(operateLogDTO,query);
    }

    @Override
    public long count(OperateLogDTO operateLogDTO) {
        Query query = buildQuery(operateLogDTO);
        return count(query);
    }

    public Query buildQuery(OperateLogDTO operateLogDTO)  {
        Query query = new Query();
        Criteria criteria = new Criteria();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(StringUtils.isNotEmpty(operateLogDTO.getBusinessType())){
            query.addCriteria(criteria.and("businessType").is(operateLogDTO.getBusinessType()));
        }
        if(StringUtils.isNotEmpty(operateLogDTO.getOperType())){
            query.addCriteria(criteria.and("operType").is(operateLogDTO.getOperType()));
        }
        if(StringUtils.isNotEmpty(operateLogDTO.getEid())){
            query.addCriteria(criteria.and("eid").is(operateLogDTO.getEid()));
        }
        if(StringUtils.isNotEmpty(operateLogDTO.getBusinessId())){
            query.addCriteria(criteria.and("businessId").is(operateLogDTO.getBusinessId()));
        }
        if(StringUtils.isNotEmpty(operateLogDTO.getOrganizationId())){
            query.addCriteria(criteria.and("organizationId").is(operateLogDTO.getOrganizationId()));
        }
        if(StringUtils.isNotEmpty(operateLogDTO.getParentId())){
            query.addCriteria(criteria.and("parentId").is(operateLogDTO.getParentId()));
        }
        if(StringUtils.isNotEmpty(operateLogDTO.getUserCode())){
            query.addCriteria(criteria.and("userCode").is(operateLogDTO.getUserCode()));
        }
        if(StringUtils.isNotEmpty(operateLogDTO.getDescription())){
            Pattern pattern = Pattern.compile("^.*" + operateLogDTO.getDescription() + ".*$");
            query.addCriteria(Criteria.where("description").regex(pattern));
        }

        // 开始时间
        if (StringUtils.isNotEmpty(operateLogDTO.getStartTime())&&StringUtils.isNotEmpty(operateLogDTO.getEndTime()))
        {
            try {
                query.addCriteria(criteria.where("createTime").gte(sdf.parse(operateLogDTO.getStartTime()+" 00:00:00")).lte(sdf.parse(operateLogDTO.getEndTime()+" 23:59:59")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Sort sort=null;
        query = query.with( sort.by(Sort.Direction.DESC, "createTime"));
        return query;
    }



}
